package net.bblot.lottery.activity;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.igexin.sdk.PushManager;
import com.umeng.socialize.UMShareAPI;

import net.bblot.lottery.api.ApiClinet;
import net.bblot.lottery.api.SimpleResponseListener;
import net.bblot.lottery.api.request.GetKey;
import net.bblot.lottery.api.request.Static;
import net.bblot.lottery.api.result.ExcuterResult;
import net.bblot.lottery.api.task.HttpTask;
import net.bblot.lottery.push.PushService;
import net.bblot.lottery.util.AppConfig;
import net.bblot.lottery.util.FileUtil;
import net.bblot.lottery.util.ZipUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;


public class QApplication extends MultiDexApplication {

    public static Context context = null;
    private static final String TAG = "QApplication";
    Handler handler = new Handler();

    /*
     * (non-Javadoc)
     *
     * @see android.app.Application#onCreate()
     */
    @SuppressWarnings("unused")
    @Override
    public void onCreate() {
        super.onCreate();
        PushManager.getInstance().initialize(this, PushService.class);
        UMShareAPI.get(this);
        context = getApplicationContext();
        //prepareWeb();
        Static.init(this);
    }

    private void prepareWeb() {
        if (!AppConfig.getWebUnzipStatus(this)) {
            boolean status = ZipUtil.unZipAssetsFile(this, "www.zip", AppConfig.getWebRootDir(this));
            if (status) {
                AppConfig.saveWebUnzipStatus(this, true);
            } else {
                FileUtil.deleteAllFile(new File(AppConfig.getWebRootDir(this)));
                AppConfig.saveWebUnzipStatus(this, false);
            }
        }
    }

    //联网获取加密时用到的密钥并保存到sp
    private void getKey() {
        SimpleResponseListener simpleResponseListener = new SimpleResponseListener() {
            @Override
            public void onGetResult(String responsCode, @NonNull final ExcuterResult er) {
                super.onGetResult(responsCode, er);
                try {
                    parseAndSave(er.getResult());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        Logger.e(TAG, "请求密钥：" + er.getResult());
//                    }
//                }, 3000);
            }
        };
        HttpTask httpTask = GetKey.getKey(this, simpleResponseListener);
        ApiClinet.getInstance().doHttpTask(httpTask, simpleResponseListener);
    }

    private void parseAndSave(String json) throws JSONException {
        if (TextUtils.isEmpty(json)) {
            return;
        }
        JSONObject jo = new JSONObject(json);
        if (jo.optString("code").equals("0000")) {
            JSONObject info = jo.optJSONObject("info");
            if (info != null) {
                String aesKey = info.optString("aesKey");
                if (!TextUtils.isEmpty(aesKey)) {
                    //保存aeskey
                    AppConfig.saveAesKey(this, aesKey);
                }

                String aesIv = info.optString("aesIv");
                if (!TextUtils.isEmpty(aesIv)) {
                    //保存aesIv
                    AppConfig.saveAesIv(this, aesIv);
                }
            }
        }
    }

}
